package com.hhsj.dynamicui.model;

import com.google.gson.annotations.SerializedName;
import com.hhsj.dynamicui.model.base.JSONAble;

import org.json.JSONObject;

import java.io.Serializable;

/**
 * 视图的基本属性
 * padding ，margin，background，圆角，尺寸，比例
 */
public final class ViewBean implements Serializable, JSONAble {

    public static final String TYPE_APPBAR = "appbar";
    public static final String TYPE_FLOATBAR = "floatbar";

    public static final String TYPE_BODY = "body";
    public static final String TYPE_GROUP_LAYOUT = "group";
    public static final String TYPE_GROUP_TITLE = "group_title";
    public static final String TYPE_WIDGET_LAYOUT = "widget_layout";
    public static final String TYPE_WIDGET_ITEM = "widget_item";

    public static final String USE_TYPE_SELF = "self";
    public static final String USE_TYPE_CHILD = "child";


    /**
     * desc : 第一组
     * backgroundType :
     * backgroundDrawable :
     * backgroundColor :
     * backgroundUrl :
     * radius : 10
     * paddingTop : 0
     * paddingBottom : 0
     * paddingLeft : -1
     * paddingRight : -1
     * marginBottom : 10
     */
    private String propertyId;
    private String desc;
    private Background background;
    private Round round;
    private Margin margin;
    private Padding padding;
    private WHSize whSize;

    public String getPropertyId() {
        return propertyId;
    }

    public void setPropertyId(String propertyId) {
        this.propertyId = propertyId;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Background getBackground() {
        return background;
    }

    public void setBackground(Background background) {
        this.background = background;
    }

    public Round getRound() {
        return round;
    }

    public void setRound(Round round) {
        this.round = round;
    }

    public Margin getMargin() {
        return margin;
    }

    public void setMargin(Margin margin) {
        this.margin = margin;
    }

    public Padding getPadding() {
        return padding;
    }

    public void setPadding(Padding padding) {
        this.padding = padding;
    }

    public WHSize getWhSize() {
        return whSize;
    }

    public void setWhSize(WHSize whSize) {
        this.whSize = whSize;
    }

    @Override
    public String toJSONString() {
        JSONObject object = new JSONObject();
        object.put("property_id", propertyId);
        object.put("owner_id", propertyId);
        JSONObject backgroundJO = new JSONObject();
        backgroundJO.put("background_type", background.getBackgroundType());
        backgroundJO.put("background_color_str", background.getBackgroundColorStr());
        backgroundJO.put("background_color_enum", background.getBackgroundColorEnum());
        object.put("background", backgroundJO);

        JSONObject roundJO = new JSONObject();
        roundJO.put("round_type", round.getRoundType());
        roundJO.put("radius", round.getRadius());
        roundJO.put("top_left", round.getCornerTopLeft());
        roundJO.put("top_right", round.getCornerTopRight());
        roundJO.put("bottom_left", round.getCornerBottomLeft());
        roundJO.put("bottom_right", round.getCornerBottomRight());
        object.put("round", roundJO);

        JSONObject whSizeJO = new JSONObject();
        whSizeJO.put("wh_relation", whSize.getWhRelation());
        whSizeJO.put("width", whSize.getWidth());
        whSizeJO.put("height", whSize.getHeight());
        whSizeJO.put("r_width", whSize.getRWidth());
        whSizeJO.put("r_height", whSize.getRHeight());
        object.put("whSize", whSizeJO);


        JSONObject marginJO = new JSONObject();
        marginJO.put("margin_left", margin.getMarginLeft());
        marginJO.put("margin_top", margin.getMarginTop());
        marginJO.put("margin_right", margin.getMarginRight());
        marginJO.put("margin_bottom", margin.getMarginBottom());
        object.put("margin", marginJO);
        JSONObject paddingJO = new JSONObject();


        paddingJO.put("padding_left", padding.getPaddingLeft());
        paddingJO.put("padding_top", padding.getPaddingTop());
        paddingJO.put("padding_right", padding.getPaddingRight());
        paddingJO.put("padding_bottom", padding.getPaddingBottom());
        object.put("padding", paddingJO);

        return object.toString();
    }

    public static class Background {


        //只有3总，0表示无，1，表示颜色int，2，表示本地的某种颜色
        @SerializedName("background_type")
        private int backgroundType;

        @SerializedName("background_color_str")
        private String backgroundColorStr;//颜色ARGB，需要解析

        @SerializedName("background_color_enum")
        private String backgroundColorEnum;//颜色名称，需要本地映射

        public int getBackgroundType() {
            return backgroundType;
        }

        public void setBackgroundType(int backgroundType) {
            this.backgroundType = backgroundType;
        }

        public String getBackgroundColorStr() {
            return backgroundColorStr;
        }

        public void setBackgroundColorStr(String backgroundColorStr) {
            this.backgroundColorStr = backgroundColorStr;
        }

        public String getBackgroundColorEnum() {
            return backgroundColorEnum;
        }

        public void setBackgroundColorEnum(String backgroundColorEnum) {
            this.backgroundColorEnum = backgroundColorEnum;
        }
    }

    public static class Margin {

        @SerializedName("margin_left")
        private int marginLeft;

        @SerializedName("margin_top")
        private int marginTop;

        @SerializedName("margin_right")
        private int marginRight;

        @SerializedName("margin_bottom")
        private int marginBottom;

        public int getMarginLeft() {
            return marginLeft;
        }

        public void setMarginLeft(int marginLeft) {
            this.marginLeft = marginLeft;
        }

        public int getMarginTop() {
            return marginTop;
        }

        public void setMarginTop(int marginTop) {
            this.marginTop = marginTop;
        }

        public int getMarginRight() {
            return marginRight;
        }

        public void setMarginRight(int marginRight) {
            this.marginRight = marginRight;
        }

        public int getMarginBottom() {
            return marginBottom;
        }

        public void setMarginBottom(int marginBottom) {
            this.marginBottom = marginBottom;
        }
    }

    public static class Padding {

        @SerializedName("padding_left")
        private int paddingLeft;

        @SerializedName("padding_top")
        private int paddingTop;

        @SerializedName("padding_right")
        private int paddingRight;

        @SerializedName("padding_bottom")
        private int paddingBottom;

        public int getPaddingTop() {
            return paddingTop;
        }

        public void setPaddingTop(int paddingTop) {
            this.paddingTop = paddingTop;
        }

        public int getPaddingBottom() {
            return paddingBottom;
        }

        public void setPaddingBottom(int paddingBottom) {
            this.paddingBottom = paddingBottom;
        }

        public int getPaddingLeft() {
            return paddingLeft;
        }

        public void setPaddingLeft(int paddingLeft) {
            this.paddingLeft = paddingLeft;
        }

        public int getPaddingRight() {
            return paddingRight;
        }

        public void setPaddingRight(int paddingRight) {
            this.paddingRight = paddingRight;
        }
    }

    public static class Round {

        @SerializedName("round_type")
        private int roundType;//0，方，1，圆，2圆角

        @SerializedName("radius")
        private int radius;//半径

        @SerializedName("top_left")
        private int cornerTopLeft;//0，关，1，开

        @SerializedName("top_right")
        private int cornerTopRight;

        @SerializedName("bottom_left")
        private int cornerBottomLeft;

        @SerializedName("bottom_right")
        private int cornerBottomRight;

        public int getRadius() {
            return radius;
        }

        public void setRadius(int radius) {
            this.radius = radius;
        }

        public int getRoundType() {
            return roundType;
        }

        public void setRoundType(int roundType) {
            this.roundType = roundType;
        }

        public int getCornerTopLeft() {
            return cornerTopLeft;
        }

        public void setCornerTopLeft(int cornerTopLeft) {
            this.cornerTopLeft = cornerTopLeft;
        }

        public int getCornerTopRight() {
            return cornerTopRight;
        }

        public void setCornerTopRight(int cornerTopRight) {
            this.cornerTopRight = cornerTopRight;
        }

        public int getCornerBottomLeft() {
            return cornerBottomLeft;
        }

        public void setCornerBottomLeft(int cornerBottomLeft) {
            this.cornerBottomLeft = cornerBottomLeft;
        }

        public int getCornerBottomRight() {
            return cornerBottomRight;
        }

        public void setCornerBottomRight(int cornerBottomRight) {
            this.cornerBottomRight = cornerBottomRight;
        }
    }

    public static class WHSize {

        @SerializedName("wh_relation")
        private int whRelation;//宽高关系模式

        @SerializedName("width")
        private int width;

        @SerializedName("height")
        private int height;

        @SerializedName("r_width")
        private float rWidth;//width/ height 宽高比 -1,不会取，>0

        @SerializedName("r_height")
        private float rHeight;//width/ height 宽高比 -1,不会取，>0

        public int getWhRelation() {
            return whRelation;
        }

        public void setWhRelation(int whRelation) {
            this.whRelation = whRelation;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }

        public int getWidth() {
            return width;
        }

        public void setWidth(int width) {
            this.width = width;
        }

        public float getRWidth() {
            return rWidth;
        }

        public void setRWidth(float rWidth) {
            this.rWidth = rWidth;
        }

        public float getRHeight() {
            return rHeight;
        }

        public void setRHeight(float rHeight) {
            this.rHeight = rHeight;
        }
    }


    /**
     * 可包含子视图的组视图属性，在otherProperties字段里，这是一个json字符串，可以转json
     */
    public static class SpecProperty implements Serializable {

        public final static int GRID_ORIENTATION_VERTICAL = 1;//默认的往下排列
        public final static int GRID_ORIENTATION_HORIZONTAL = 2;//横向滚动的
        public final static int BANNER_TYPE_PAGE = 1;//单个item的banner
        public final static int BANNER_TYPE_GRID = 2;//网格作为item的banner
        public final static int TABLE_TYPE_LIST = 1;//list
        public final static int TABLE_TYPE_GRID = 1;//grid


        //这两条数据business,itemWidgetType不是他自身的，是由父节点传入的
        private String business;//子视图的业务
        private String itemWidgetType;//子视图样式
        private ViewBean property;
        private String desc;//相关描述

        public String getBusinessCode() {
            return business;
        }

        public void setBusinessCode(String businessCode) {
            this.business = businessCode;
        }

        public String getItemWidgetCode() {
            return itemWidgetType;
        }

        public void setItemtWidgetCode(String itemtWidgetCode) {
            this.itemWidgetType = itemtWidgetCode;
        }

        public ViewBean getProperty() {
            return property;
        }

        public void setProperty(ViewBean property) {
            this.property = property;
        }

    }


    /**
     * banner grid 属性 这个不支持自动轮播和循环轮播
     */
    public static final class BannerGridConfig extends SpecProperty {
        //banner指示器样式
        @SerializedName("indicator_type")
        public int indicatorType;
        //切换动画样式

        @SerializedName("animator_type")
        public int animationType;
        //行数

        @SerializedName("max_row")
        public int maxRow;
        //列数

        @SerializedName("max_column")
        public int maxColumn;


    }


    /**
     * banner属性
     */
    public static final class BannerPagerConfig extends SpecProperty {
        //banner指示器样式0，没有指示器，1，数字，2，圆点，3 圆角
        @SerializedName("indicator_type")
        public int indicatorType;
        //切换动画样式0，没有动画，123等对应各自类型动画

        @SerializedName("animator_type")
        public int animationType;
        //自动 0，不自动，1自动

        @SerializedName("auto_play")
        public int autoPlay;
        //循环 0，不循环，1循环

        @SerializedName("recycle")
        public int recycle;
        //间隔时间

        @SerializedName("duration")
        public int duration;

    }


    /**
     * 列表属性
     */
    public static class ListConfig extends SpecProperty {
        //测量高度还是自适应高度，0 自适应高度，1，固定高度

        public int diverHeight;
        public int diverLeftMargin;
        public int diverRightMargin;
        public String diverColorStr;
        public int heightType;


    }


    /**
     * 网格属性
     */
    public static class GridConfig extends SpecProperty {
        public int maxRow;
        public int maxColumn;
        public int horizontalSpace;
        public int verticalSpace;
    }


    /**
     * 流式布局
     */
    public static class FlowConfig extends SpecProperty {
        public int orientation;//方向，从左到右从上到小，换行，从上到下，从左到右换列。

    }

    /**
     * 头条控件属性
     */
    public static class TopLineConfig extends SpecProperty {
        //头条样式
        public int topLineType; //1，左图标右文字，2，左标题右title，3，做标题，右title，desc
        //轮播间隔
        public int duration;

    }


    public WidgetProperty toWidgetProperty() {
        WidgetProperty.Builder builder = new WidgetProperty.Builder();

        if (null != whSize) {
            builder
                    .setHeight(whSize.height)
                    .setWidth(whSize.width)
                    .setWhRelation(whSize.whRelation)
                    .setrWidth(whSize.rWidth)
                    .setrHeight(whSize.rHeight);
        }
        if (null != round) {
            builder
                    .setRoundType(round.roundType)
                    .setRadius(round.radius)
                    .setCornerBottomLeft(round.cornerBottomLeft == 1)
                    .setCornerBottomRight(round.cornerBottomRight == 1)
                    .setCornerTopLeft(round.cornerTopLeft == 1)
                    .setCornerTopRight(round.cornerTopRight == 1);

        }
        if (null != padding) {
            builder
                    .setPaddingLeft(padding.paddingLeft)
                    .setPaddingTop(padding.paddingTop)
                    .setPaddingRight(padding.paddingRight)
                    .setPaddingBottom(padding.paddingBottom);

        }
        if (null != margin) {
            builder
                    .setMarginLeft(margin.marginLeft)
                    .setMarginTop(margin.marginTop)
                    .setMarginRight(margin.marginRight)
                    .setMarginBottom(margin.marginBottom);

        }

        if (null != background) {
            builder
                    .setBackgroundType(background.backgroundType)
                    .setBackgroundColorEnum(background.backgroundColorEnum)
                    .setBackgroundColorStr(background.backgroundColorStr)
                    .create();
        }

        return builder.create();

    }


}
